<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0, minimum-scale=1.0, user-scalable=0, initial-scale=1.0, width=device-width"/>
   <meta name="format-detection" content="telephone=no, email=no, date=no, address=no">
    <title>个人信息</title>
     <link rel="stylesheet" type="text/css" href="../../css/bootstrap.min.css" />
     <link rel="stylesheet" href="../../css/edit.css">
    <script type="text/javascript" src="../../script/api.js"></script>
    <script type="text/javascript" src="../../script/jquery-3.2.1.min.js"></script>

    <script type="text/javascript" src="../../script/main.js"></script>
     <script type="text/javascript" src="../../script/bootstrap.js"></script>
  </head>
  <body>
    <div class="header">
    <div class="back" onclick="closeWin()"></div>
      <h3>编辑简历</h3>
    </div>
    <div class="box">
      <div class="spanInput">
        <span>昵称(选)：</span>
        <span><input type="text" name="1" value="1" id="nickname"></span>
      </div>
      <div class="spanInput">
        <span>真实姓名：</span>
        <span><input type="text" name="2" value="2" id="realname"></span>
      </div>
      <div class="spanInput">
        <span>性别：</span>
        <span>
       <select><option class="sex" value='0'>女</option><option class="sex" value='1'>男</option></select>
        </span>
      </div>
      <div class="spanInput">
        <span>生日：</span>
        <span><input type="text" name="" value="" id="birthday"></span>
      </div>
      <div class="spanInput">
        <span>学号(选)：</span>
        <span><input type="text" name="" value="" id="studentId"></span>
      </div>
      <div class="spanInput">
        <span>身份证号：</span>
        <span><input type="text" name="" value="" id="idCard"></span>
      </div>
      <div class="spanInput">
        <span>地址(选)：</span>
        <span><input type="text" name="" value="" id="address"></span>
      </div>
      <div class="spanInput">
        <span>求职意向(选)：</span>
        <span><input type="text" name="" value="" id="function"></span>
      </div>
      <div class="spanInput">
        <span>求职状态(选)：</span>
        <span><input type="text" name="" value="" id="jobStatus"></span>
      </div>
      <div class="spanInput">
        <span>就业区域(选)：</span>
        <span><input type="text" name="" value="" id="workPlace"></span>
      </div>
      <div>
            <span>* (选)标明的为选填项，为了信息的完整，建议全部填写！</span>
        </div>
    </div>
    <footer>
 <button class="submit">提交</button>
 </footer>
  </body>
  <script type="text/javascript">
  var age;

     apiready=function(){
       var aid=$api.getStorage('aid');
       var url='/api/paTechnicaluser/'+aid;
       var functionRet=function(ret){

         if(ret.birthday!=""){
      age=ret.age;
    }else{
      age=0;
    }
  $("#nickname").val(ret.nickname);
  $("#realname").val(ret.realName);
  if(ret.sex==0){
    $("select").val(0);
  }else{
    $("select").val(1);
  }
  $("#birthday").val(ret.birthday);
  $("#studentId").val(ret.studentId);
  $("#idCard").val(ret.idCard);
  $("#address").val(ret.address);
  $("#function").val(ret.function);
  $("#jobStatus").val(ret.jobStatus);
  $("#workPlace").val(ret.workPlace);
}
var functionErr=function(err){
  toast("加载原有简历资料失败！","bottom");
}
get(url,functionRet,functionErr);
    }

    // $("input").blur(function(){
    //   var nickname = $(this).value;
    //   console.log(nickname);
    // })
//提交
    $(".submit").click(function(){
       var nickname =$.trim($("#nickname").val());
       var realname=$.trim($("#realname").val());
       var sex = $("select").val();
       console.log(sex);//0,1
       var birthday = $("#birthday").val();
       var studentId =$.trim($("#studentId").val());
       var idCard = $.trim($("#idCard").val());//提交的数据去掉前后的空格
       var address = $.trim($("#address").val());
       var _function = $.trim($("#function").val());
       var jobStatus = $.trim($("#jobStatus").val());
       var workPlace =$.trim($("#workPlace").val());
       $("input").removeClass("borderWarning");


      if(nickname==0||nickname.length>6){
        $("#nickname").addClass("borderWarning");
        toast("昵称长度1-6个数字字母或汉字，请重新填写","bottom");
        return false;
      }
     if(RegeMatch()){
   $("#realname").addClass("borderWarning");
   toast("真实姓名只能输入6位以内汉字，不含字母数字和特殊字符，请重新填写","bottom");
   return false;
       }

       if(studentId != "" && studentId!= null){
         var set_number = /^[a-zA-Z0-9_]{6,20}$/;
        if(set_number.test(studentId)==false){
            $("#studentId").addClass("borderWarning");
            toast("学号长度请控制在6-20位的数字或字母，请重新填写","bottom");
            return false;
          }
       }

       if(address.length>20){
           $("#address").addClass("borderWarning");
           toast("地址长度请控制在20字以内，请重新填写","bottom");
           return false;
         }
         if(_function.length>20){
           $("#function").addClass("borderWarning");
           toast("求职意向长度请控制在20字以内，请重新填写","bottom");
           return false;
         }
         if(jobStatus.length>20){
           $("#jobStatus").addClass("borderWarning");
           toast("求职状态长度请控制在20字以内，请重新填写","bottom");
           return false;
         }
         if(workPlace.length>20){
           $("#workPlace").addClass("borderWarning");
           toast("就业区域长度请控制在20字以内，请重新填写","bottom");
           return false;
         }
         if(realname!=0){

             if(age>0){

               if(IdCardValidate(idCard)&&(idCard!=0)){
                 var jsons={
                             "nickname":nickname,"realName":realname,"sex":sex,"age":age,"birthday":birthday,"studentId":studentId,"idCard":idCard,"address":address,
                             "function":_function,"jobStatus":jobStatus,"workPlace":workPlace
                         };
                         var aid=$api.getStorage('aid');
                         var url='/api/paTechnicaluser/'+aid;
                         var functionRet=function(ret){
                         }
                         var functionErr=function(err){
                           console.log(err.statusCode);
                           if (err.statusCode==200) {


                               api.execScript({
                                 name: 'personInformation',
                                 script: 'location.href = "personInformation.html";toast("编辑资料成功！","bottom");'
                               });
                               api.execScript({
                                 name: 'root',
                                 frameName:'frame3',
                                 script: 'window.location.reload();toast("成功！","top");'
                               });
                               api.closeWin({
                                   name: 'edit',

                               });
                           }
                           else {
                             console.log(err.statusCode);
                               toast("编辑资料失败！","bottom");
                           }
                         }
                         put(url,jsons,functionRet,functionErr);
               }else {
                 $("#idCard").addClass("border_warning");
                 toast("再检查一下身份证吧","bottom");
               }

           }else{
             $("#birthday").addClass("borderWarning");
             toast("日期超前，请重新选择","bottom");
           }


         }else{
           $("#realname").addClass("borderWarning");
           toast("真实姓名不能为空，请完善","bottom");
         }

    })

  //真实姓名特殊符号限制
  function RegeMatch(){
     var realname=$.trim($("#realname").val());
      var pattern = /^[\u4E00-\u9FA5]{1,6}$/;
      if(realname != "" && realname!= null){
          if(!pattern.test(realname)){
              return true;
          }
      }
  }

//出生日期
var date;
  $("#birthday").click(function(){
  showCalendar();
});
  function showCalendar(){
    api.openPicker({
      type: 'date',
      date : date,
      title: '选择时间'
    }, function(ret, err) {
      console.log(JSON.stringify(ret));
        if (ret) {
          var month,day;
          if(ret.month<10){
            month='0'+ret.month;
          }else{
            month=ret.month;
          }
          if(ret.day<10){
            day='0'+ret.day;
          }else{
            day=ret.day;
          }
            $("#birthday").val( ret.year + '-' + month + '-' + day);
            var now = new Date();
            var nowYear = now.getFullYear();
            var nowMonth = now.getMonth();
            var nowDate = now.getDate();
            age=nowYear-ret.year;
            // age[1]=nowMonth-ret.month+1;
            // age[2]=nowDate-ret.day;
            //  var flag=true;
            // if(age[0]==0 ){
            //     if(age[1]==0){
            //       if(age[2]>=0) {
            //         flag=true;
            //       }else flag=false;
            //     }else if(age[1]>0){
            //        flag=true;
            //     }else flag=false;
            // }else if(age[0]>0){
            //   flag=true;
            // }else flag=false;
            //

            // console.log(nowDate);
            // console.log(ret.day);
            // console.log(day);
            // console.log(age);
        } else {
            alert(JSON.stringify(err));
        }
    });
  }

//验证身份证正确性
var Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 ];   // 加权因子
var ValideCode = [ 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 ];            // 身份证验证位值.10代表X
function IdCardValidate(idCard) {
  console.log(idCard);
    idCard = $.trim(idCard);
      console.log(idCard);            //去掉字符串头尾空格
    if (idCard.length == 15) {
        return isValidityBrithBy15IdCard(idCard);       //进行15位身份证的验证
    } else if (idCard.length == 18) {
        var a_idCard = idCard.split("");                // 得到身份证数组
        if(isValidityBrithBy18IdCard(idCard)&&isTrueValidateCodeBy18IdCard(a_idCard)){   //进行18位身份证的基本验证和第18位的验证
            return true;
        }else {
            return false;
        }
    } else {
        return false;
    }
}
/**
 * 判断身份证号码为18位时最后的验证位是否正确
 * @param a_idCard 身份证号码数组
 * @return
 */
function isTrueValidateCodeBy18IdCard(a_idCard) {
    var sum = 0;                             // 声明加权求和变量
    if (a_idCard[17].toLowerCase() == 'x') {
        a_idCard[17] = 10;                    // 将最后位为x的验证码替换为10方便后续操作
    }
    for ( var i = 0; i < 17; i++) {
        sum += Wi[i] * a_idCard[i];            // 加权求和
    }
    valCodePosition = sum % 11;                // 得到验证码所位置
    if (a_idCard[17] == ValideCode[valCodePosition]) {
        return true;
    } else {
        return false;
    }
}
/**
  * 验证18位数身份证号码中的生日是否是有效生日
  * @param idCard 18位书身份证字符串
  * @return
  */
function isValidityBrithBy18IdCard(idCard18){
    var year =  idCard18.substring(6,10);
    var month = idCard18.substring(10,12);
    var day = idCard18.substring(12,14);
    var temp_date = new Date(year,parseFloat(month)-1,parseFloat(day));
    // 这里用getFullYear()获取年份，避免千年虫问题
    if(temp_date.getFullYear()!=parseFloat(year)
          ||temp_date.getMonth()!=parseFloat(month)-1
          ||temp_date.getDate()!=parseFloat(day)){

            return false;
    }else{
        return true;
    }
}
  /**
   * 验证15位数身份证号码中的生日是否是有效生日
   * @param idCard15 15位书身份证字符串
   * @return
   */
  function isValidityBrithBy15IdCard(idCard15){
      var year =  idCard15.substring(6,8);
      var month = idCard15.substring(8,10);
      var day = idCard15.substring(10,12);
      var temp_date = new Date(year,parseFloat(month)-1,parseFloat(day));
      // 对于老身份证中的你年龄则不需考虑千年虫问题而使用getYear()方法
      if(temp_date.getYear()!=parseFloat(year)
              ||temp_date.getMonth()!=parseFloat(month)-1
              ||temp_date.getDate()!=parseFloat(day)){
                return false;
        }else{
            return true;
        }
  }

  </script>
</html>
